﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI.Controls
{
    public class GalleryItemEventArgs : EventArgs
    {
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public GalleryItemEventArgs(int id)
        {
            this.id = id;
        }
    }

    public class GalleryEditItemEventArgs : EventArgs
    {
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public GalleryEditItemEventArgs(int id, string newName)
        {
            this.id = id;
            this.NewName = newName;
        }

        string newName;

        public string NewName
        {
            get { return newName; }
            set { newName = value; }
        }
    }

    public delegate void DeletePictureEventHandler(object sender, GalleryItemEventArgs e);
    public delegate void SetDefaultPictureEventHandler(object sender, GalleryItemEventArgs e);
    public delegate void RenamePictureEventHandler(object sender, GalleryEditItemEventArgs e);

    public partial class LightBoxGallery : System.Web.UI.UserControl
    {
        public event DeletePictureEventHandler DeletePicture;
        public event SetDefaultPictureEventHandler SetDefaultPicture;
        public event RenamePictureEventHandler RenamePicture;

        public virtual void OnDeletePicture(GalleryItemEventArgs e)
        {
            if (DeletePicture != null)
                DeletePicture(this, e);
        }

        public virtual void OnSetDefaultPicture(GalleryItemEventArgs e)
        {
            if (SetDefaultPicture != null)
                SetDefaultPicture(this, e);
        }

        public virtual void OnRenamePicture(GalleryEditItemEventArgs e)
        {
            if (RenamePicture != null)
                RenamePicture(this, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public NeedDataSource OnNeedDataSource
        {
            get;
            set;
        }

        public virtual void RefreshData()
        {
            if (OnNeedDataSource != null)
            {
                EventArgs e = new EventArgs();
                OnNeedDataSource(this, e);
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), "initLightbox();", true); 
            }

            lblNoRecord.Visible = rptImages.Items.Count <= 0;
        }

        protected override void OnLoad(EventArgs e)
        {
            ScriptManager.RegisterClientScriptInclude(this, typeof(Page), Guid.NewGuid().ToString(), "Script/prototype.js");
            ScriptManager.RegisterClientScriptInclude(this, typeof(Page), Guid.NewGuid().ToString(), "Script/scriptaculous.js?load=effects");
            ScriptManager.RegisterClientScriptInclude(this, typeof(Page), Guid.NewGuid().ToString(), "Script/lightbox.js");
        }

        string originalField;

        public string OriginalField
        {
            get { return originalField; }
            set { originalField = value; }
        }

        string thumbField;

        public string ThumbField
        {
            get { return thumbField; }
            set { thumbField = value; }
        }

        string defaultField;

        public string DefaultField
        {
            get { return defaultField; }
            set { defaultField = value; }
        }

        string descriptionField;

        public string DescriptionField
        {
            get { return descriptionField; }
            set { descriptionField = value; }
        }

        string idField;

        public string IDField
        {
            get { return idField; }
            set { idField = value; }
        }

        public Object DataSource
        {
            get { return rptImages.DataSource; }
            set { rptImages.DataSource = value; rptImages.DataBind(); }
        }

        public int RepeatColumns
        {
            get { return rptImages.RepeatColumns; }
            set { rptImages.RepeatColumns = value; }
        }

        public RepeatDirection RepeatDirection
        {
            get { return rptImages.RepeatDirection; }
            set { rptImages.RepeatDirection = value; }
        }

        public RepeatLayout RepeatLayout
        {
            get { return rptImages.RepeatLayout; }
            set { rptImages.RepeatLayout = value; }
        }

        public int ItemCount
        {
            get { return rptImages.Items.Count; }
        }

        protected void btnDeletePic_OnClick(object sender, EventArgs e)
        {
            OnDeletePicture(new GalleryItemEventArgs(int.Parse(hidSelectedPic.Value)));
            RefreshData();
        }

        protected void btnSetDefault_OnClick(object sender, EventArgs e)
        {
            OnSetDefaultPicture(new GalleryItemEventArgs(int.Parse(hidSelectedPic.Value)));
            RefreshData();
        }

        protected void btnRefresh_OnClick(object sender, EventArgs e)
        {
            RefreshData();
        }

        protected void btnSaveName_OnClick(object sender, EventArgs e)
        {
            OnRenamePicture(new GalleryEditItemEventArgs(int.Parse(hidEditId.Value), hidNewName.Value));
            hidEditId.Value = "";
            RefreshData();
        }
    }
}